package org.yate.oa.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.yate.oa.model.OrgEntity;
import org.yate.oa.model.UserEntity;
import org.yate.oa.service.IOrgService;
import org.yate.oa.service.IRelationService;
import org.yate.oa.service.imp.OrgService;
import org.yate.oa.service.imp.RelationService;
import org.yate.oa.service.imp.UserService;
import org.yate.oa.utils.Pagination;

import com.alibaba.fastjson.JSONObject;

@WebServlet(urlPatterns = { "/org/get", "/org/post", "/org/put", "/org/delete" }, asyncSupported = false)
public class OrgController extends BaseServletController {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
		doPost(req, rsp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
		String type = req.getParameter("type");
		PrintWriter out = rsp.getWriter();
		JSONObject json = new JSONObject();
		if (StringUtils.isEmpty(type)) {
			json.put("code", "401");
			json.put("msg", "type cannot be null");
			out.println(json);
			out.flush();
			out.close();
			return;
		}
		if (type.equals("all")) {
			getAllOrgs(req, rsp);
		} else if (type.equals("get")) {
			getByOrgId(req, rsp);
		} else if (type.equals("add")) {
			add(req, rsp);
		} else if (type.equals("getOrdStaff")) {
			getOrdStaff(req, rsp);
		} else if (type.equals("modify")) {
			modifyOrg(req, rsp);
		} else if (type.equals("delete")) {
			deleteOrg(req, rsp);
		}
	}

	private void getAllOrgs(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
		PrintWriter out = rsp.getWriter();
		JSONObject json = new JSONObject();
		IOrgService service = this.springCtx.getBean(OrgService.class);
		List<OrgEntity> entities = service.getEntityAll();
		json.put("code", "200");
		json.put("data", entities);
		out.println(json);
		out.flush();
		out.close();

	}

	private void add(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
		PrintWriter out = rsp.getWriter();
		JSONObject json = new JSONObject();
		Integer parentOrgId = Integer.parseInt(req.getParameter("parentOrg"));
		String orgName = req.getParameter("orgName");
		String description = req.getParameter("description");
		Integer managerId = Integer.parseInt(req.getParameter("orgHeadId"));
		OrgEntity e = new OrgEntity(0, parentOrgId, orgName, description);
		e.setManagerId(managerId);
		IOrgService service = this.springCtx.getBean(OrgService.class);
		service.add(e);
		json.put("code", "200");
		json.put("entity", e);
		json.put("msg", "add org success");
		out.println(json);
		out.flush();
		out.close();
	}

	private void getByOrgId(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
		PrintWriter out = rsp.getWriter();
		JSONObject json = new JSONObject();
		Integer orgId = Integer.parseInt(req.getParameter("orgId"));
		if (orgId < 0) {
			json.put("code", "401");
			json.put("msg", "orgId cannot be null");
			out.println(json);
			out.flush();
			out.close();
			return;
		}
		IOrgService service = this.springCtx.getBean(OrgService.class);
		OrgEntity e = service.getEntity(orgId);
		UserEntity userEntity = null;
		if (e != null) {
			UserService userService = this.springCtx.getBean(UserService.class);
			if (e.getManagerId() != null) {
				userEntity = userService.getEntity(e.getManagerId());
			}
		}
		json.put("code", "200");
		json.put("data", e);
		json.put("user", userEntity);
		out.println(json);
		out.flush();
		out.close();

	}

	/**
	 * 获取组织的员工
	 * 
	 * @param req
	 * @param res
	 * @param orgId
	 * @throws IOException
	 */
	protected void getOrdStaff(HttpServletRequest req, HttpServletResponse res) throws IOException {
		String page = req.getParameter("page");
		String orgId = req.getParameter("orgId");
		int pageIndex = 0;
		if (null != page) {
			pageIndex = Integer.parseInt(page);
		}
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		UserService userService = this.springCtx.getBean(UserService.class);
		String[] strings = { orgId };
		Pagination<UserEntity> data = userService.getEntitiesByOrdId(pageIndex, 8, "", "", strings);
		json.put("status", 200);
		json.put("data", data);
		out.println(json);
		out.flush();
		out.close();
	}

	private void modifyOrg(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
		PrintWriter out = rsp.getWriter();
		JSONObject json = new JSONObject();
		Integer orgId = Integer.parseInt(req.getParameter("orgId"));
		String des = req.getParameter("orgDes");
		Integer head = Integer.parseInt(req.getParameter("orgHead"));
		String orgName = req.getParameter("orgName");
		OrgEntity e = new OrgEntity();
		e.setId(orgId);
		e.setDescription(des);
		e.setOrgName(orgName);
		e.setManagerId(head);
		IOrgService svr = this.springCtx.getBean(OrgService.class);
		svr.update(e);
		json.put("status", 200);
		json.put("data", "modify org success");
		out.println(json);
		out.flush();
		out.close();
	}

	private void deleteOrg(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
		PrintWriter out = rsp.getWriter();
		JSONObject json = new JSONObject();
		IOrgService svr = this.springCtx.getBean(OrgService.class);
		Integer orgId = Integer.parseInt(req.getParameter("orgId"));
		OrgEntity oe = svr.getEntity(orgId);
		Map<String,Integer> map = new HashMap<String, Integer>();
		map.put("newOrgId", oe.getParentOrgId());
		map.put("oldOrgId", orgId);
		IRelationService rsvr = this.springCtx.getBean(RelationService.class);
		rsvr.updateBatch(map);
		svr.remove(orgId);
		json.put("code", 200);
		json.put("msg", "remove org success");
		out.println(json);
		out.flush();
		out.close();
	}
}